package com.jpxx.netty.helloAandB;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;

  
public class ClientHandler extends ChannelHandlerAdapter{  

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		try {
			//msg默认就是比buffer类型
			ByteBuf buf = (ByteBuf)msg;
			byte[] req = new byte[buf.readableBytes()];
			buf.readBytes(req);
			 
			String body = new String(req,"utf-8");
			System.out.println("Client客户端:"+body);
		} finally{
			//根服务端的区别，不释放的话数据会撑爆（必须）
			//服务端有写操作会自动释放，只有读没有写操作就需要手动释放
			ReferenceCountUtil.release(msg);//ByteBuf释放它
		}
		
	}
 
	//当这个通道激活的时候
	@Override  
	public void channelActive(ChannelHandlerContext ctx) {  
		//System.out.println("客户端启动,channelActive！");  
	}  

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		//System.out.println("读取完毕");  
	}

	@Override  
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {  
		cause.printStackTrace();  
		ctx.close();  
	}  

}  
